import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}
///https://blog.csdn.net/mengks1987/article/details/109173508 教程
class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var channel = MethodChannel('flutter_plugin_params');
  var _data;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(backgroundColor: Colors.white,title: Text(widget.title)),
      body: Container(
        width: double.infinity,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            OutlinedButton(
                onPressed: () async {
                  var result = await channel.invokeMethod('sendData', {'name': 'chen', 'age': 20});
                  var name = result['name'];
                  var age = result['age'];
                  _data = '$name,$age';
                  setState(() {});
                  print(_data);
                },
                child: Text('与原生通信')),
            Text('原生数据返回：$_data')
          ],
        ),
      ),
    );
  }
}
